import utils_index from '../../utils/index.js';
const regeneratorRuntime = utils_index.regeneratorRuntime
import {
  itemModel
} from '../../apis/index.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: [{
        img1: '/images/shuamai1.png',
        img2: '/images/shuamai2.png',
        style1: 'width:162rpx;height: 44rpx;',
        style2: 'width:142rpx;height: 34rpx;',
        type:0
      },
      {
        img1: '/images/womai1.png',
        img2: '/images/womai2.png',
        style2: 'width:162rpx;height: 44rpx;',
        style1: 'width:142rpx;height: 34rpx;',
        type: 0
      }
    ],
    tabIndex:0,
    lists:[{type:false}],
    list1:[],
    list2:[],
    page1:{
      page:1,
      pageSize:10
    },
    page2:{
      page:1,
      pageSize:10
    },
  },
  mixins: [require('../../myMixin.js')],
  clickTab(e){
    let { tabList} = this.data
    let _this = this
    let data = utils_index.getDataSet(e)
    let object = `tabList${data.index}.type`
    tabList.forEach((val,index)=>{
      let object = `tabList[${index}].type`
      _this.setData({
        [object]:data.index
      })
    })
    this.setData({
      tabIndex:data.index
    })
    this.getInitList()
  },
  clickList(e) {
    let { list1,list2 } = this.data
    let _this = this
    let data = utils_index.getDataSet(e)
    let lists = this.data.tabIndex==0?list1:list2
    let object = `lists[${data.index}].type`
    let type = lists[data.index].type
    this.setData({
      [object]:!type
    })
  },
  onLoad: function(options) {

  },
  onShow: function() {
    this.getInitList()
  },
  onHide(){
    this.getUnreadCount()
  },
  moreList1Data(){
    this.loadMoreData('page',this.getSalesList)
  },
  moreList2Data(){
    this.loadMoreData('page',this.getPurchaseList)
  },
  getInitList(){
    let {tabIndex,list1,list2} = this.data
    if(tabIndex ==0 && !list1.length){
      this.getSalesList()
    }else if(tabIndex ==1 && !list2.length){
      this.getPurchaseList()
    }
  },
  getSalesList(){
    let {page1,list1} = this.data
    let param = {
      page:page1.page,
      pageSize:page1.pageSize
    }
    utils_index.showLoading()
    itemModel.saledLeases(param).then(res=>{
      let _list = res.list
      _list.forEach(el=>{
        el.type=false
      })
      list1 = list1.concat(_list)
      this.computedPage('page1',res.total)
      this.setData({
        list1:list1
      })  
      utils_index.hideAll()
    }).catch(()=>{
      utils_index.hideAll()
    })
  },
  getPurchaseList(){
    let {page2,list2} = this.data
    let param = {
      page:page2.page,
      pageSize:page2.pageSize
    }
    utils_index.showLoading()
    itemModel.purchasedLeases(param).then(res=>{
      let _list = res.list
      _list.forEach(el=>{
        el.type=false
      })
      list2 = list2.concat(_list)
      this.computedPage('page2',res.total)
      this.setData({
        list2:list2
      })  
      utils_index.hideAll()
    }).catch(()=>{
      utils_index.hideAll()
    })
  }
})